package gestionBulletins.controller;
import gestionBulletins.model.Adherent;
import gestionBulletins.model.persistance.DaoAdherent;
import gestionBulletins.model.persistance.DaoFactory;
import gestionBulletins.model.persistance.DaoFactoryBuilder;

import java.sql.*;
import java.util.ArrayList;

public class Test {

	public static void test1() throws Exception {
		Class.forName("org.sqlite.JDBC");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:bdd.sqlite");
		Statement st = conn.createStatement();
		//st.executeUpdate("drop table if exists person");
		//st.executeUpdate("create table person (id integer, nom string, prenom string)");
		//st.executeUpdate("insert into person (id,nom,prenom) values (1,'Wohler','Paraita');");
		//st.executeUpdate("insert into person (id,nom,prenom) values (2,'Iriti','Moeanui');");
		ResultSet rs = st.executeQuery("SELECT * FROM adherent");
		ResultSetMetaData rsmd = rs.getMetaData();
		int nbCols = rsmd.getColumnCount();
		for (int i = 1; i <= nbCols; i++) {
			System.out.print(rsmd.getColumnName(i) + " ");
		}
		System.out.println("\n");
		while (rs.next()) {
			for(int i = 1; i <= nbCols; ++i) {
				String s = rs.getString(i);
				if (s != null) {
					System.out.print(s + " ");
				}
				else {
					System.out.print("\t");	
				}
			}
			System.out.println("\n");
		}
		rs.close();
		conn.close();
	}
	
	public static void test2() {
		DaoFactoryBuilder.setTypePersistance(DaoFactoryBuilder.TypePersistance.Sqlite);
		DaoFactory df = DaoFactoryBuilder.getDaoFactory();
		DaoAdherent da = df.getDaoAdherent();
		Adherent a1 = new Adherent("Mlle", "Iriti", "Moeanui", "22", "1", "1989");
		a1 = da.create(a1);
		System.out.println("création terminée !");
	}
	
	public static void test3() {
		DaoFactoryBuilder.setTypePersistance(DaoFactoryBuilder.TypePersistance.Sqlite);
		DaoFactory df = DaoFactoryBuilder.getDaoFactory();
		System.out.println("récupération de tout les adhérents:");
		DaoAdherent da = df.getDaoAdherent();
		ArrayList<Adherent> ca = da.getAll();
		for (Adherent a : ca) {
			System.out.println(a);
		}
	}
	
	public static void test4() {
		DaoFactoryBuilder.setTypePersistance(DaoFactoryBuilder.TypePersistance.Sqlite);
		DaoFactory df = DaoFactoryBuilder.getDaoFactory();
		DaoAdherent da = df.getDaoAdherent();
		System.out.println("update de Moeanui:");
		Adherent mo = da.requestById(1);
		mo.setNom("Iriti");
		da.update(mo);
		System.out.println(mo);
	}
	
	
	public static void main(String[] args) throws Exception {
		//test1();
		//test2();
		test3();
		test4();
	}

}
